package architecture.demo12;



import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private static final int SERVER_PORT = 1234;

    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT)) {
            System.out.println("服务器已启动。");

            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("客户端 " + socket.getInetAddress().getHostAddress() + " 已连接。");
                new Thread(new ClientHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class ClientHandler implements Runnable {
        private Socket socket;
        private List<Contact> contacts;

        public ClientHandler(Socket socket) {
            this.socket = socket;
            this.contacts = new ArrayList<>();
        }

        @Override
        public void run() {
            try (ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                 ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream())) {

                boolean isExit = false;

                while (!isExit) {
                    // 接收客户端发送的命令
                    String command = inputStream.readUTF();

                    switch (command) {
                        case "add":
                            // 接收客户端发送的联系人信息
                            String contactInfo = inputStream.readUTF();
                            String[] fields = contactInfo.split(" ");
                            Contact contact = new Contact(fields[0], fields[1], fields[2]);

                            // 添加联系人到列表
                            contacts.add(contact);
                            outputStream.writeUTF("联系人添加成功。");
                            outputStream.flush();
                            break;

                        case "search":
                            // 接收客户端发送的关键词
                            String keyword = inputStream.readUTF();

                            // 查找符合条件的联系人
                            List<Contact> searchResults = new ArrayList<>();
                            for (Contact c : contacts) {
                                if (c.getName().contains(keyword)
                                        || c.getAddress().contains(keyword)
                                        || c.getPhoneNumber().contains(keyword)) {
                                    searchResults.add(c);
                                }
                            }

                            // 将搜索结果发送给客户端
                            outputStream.writeObject(searchResults);
                            outputStream.flush();
                            break;

                        case "update":
                            // 接收客户端发送的联系人信息和当前姓名
                            String updateInfo = inputStream.readUTF();
                            String[] updateFields = updateInfo.split(" ");
                            Contact updateContact = new Contact(updateFields[0], updateFields[1], updateFields[2]);

                            // 在联系人列表中查找要修改的联系人
                            Contact target = null;
                            for (Contact c : contacts) {
                                if (c.getName().equals(updateFields[3])) {
                                    target = c;
                                    break;
                                }
                            }

                            // 如果找到了目标联系人，则更新其信息
                            if (target != null) {
                                target.setName(updateContact.getName());
                                target.setAddress(updateContact.getAddress());
                                target.setPhoneNumber(updateContact.getPhoneNumber());
                                outputStream.writeUTF("联系人修改成功。");
                            } else {
                                outputStream.writeUTF("没有找到要修改的联系人。");
                            }
                            outputStream.flush();
                            break;

                        case "delete":
                            // 接收客户端发送的要删除的联系人姓名
                            String deleteName = inputStream.readUTF();

                            // 在联系人列表中查找要删除的联系人
                            Contact deleteTarget = null;
                            for (Contact c : contacts) {
                                if (c.getName().equals(deleteName)) {
                                    deleteTarget = c;
                                    break;
                                }
                            }

                            // 如果找到了目标联系人，则从列表中删除
                            if (deleteTarget != null) {
                                contacts.remove(deleteTarget);
                                outputStream.writeUTF("联系人删除成功。");
                            } else {
                                outputStream.writeUTF("没有找到要删除的联系人。");
                            }
                            outputStream.flush();
                            break;

                        case "exit":
                            isExit = true;
                            break;

                        default:
                            outputStream.writeUTF("无效命令。");
                            outputStream.flush();
                            break;
                    }
                }

                // 关闭连接
                socket.close();
                System.out.println("客户端 " + socket.getInetAddress().getHostAddress() + " 已断开。");

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

